package com.gxlevi.util;

import org.apache.kafka.clients.producer.Partitioner;
import org.apache.kafka.common.Cluster;

import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

public class MyKafkaPartition implements Partitioner {

    AtomicInteger counter = new AtomicInteger(0);

    @Override
    public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) {
        Integer partitions = cluster.partitionCountForTopic(topic);
        int partition = counter.incrementAndGet() % partitions;
        if (counter.get() > 65535) {
            counter.set(0);
        }
        return partition;
    }

    @Override
    public void close() {

    }

    @Override
    public void configure(Map<String, ?> configs) {

    }
}
